package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.WineCouponsService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.UserWineCouponsDao;
import com.qdlc.p2p.dal.dao.WineCouponsDao;
import com.qdlc.p2p.dal.dto.WineCoupons;
import com.qdlc.p2p.dal.model.WineCouponsModel;

/**
 * 酒券方案 Service
 *
 * @author ygw
 * @version 1.1.0.0
 */
@Service("wineCouponsService")
public class WineCouponsServiceImpl implements WineCouponsService {

    @Resource
    private WineCouponsDao wineCouponsDao;

    @Resource
    private UserWineCouponsDao userWineCouponsDao;

    @Override
    public WineCoupons save(WineCouponsModel model) {
        // 保存酒券方案
    	WineCoupons wineCoupons = packageWineCoupons(model);
    	wineCoupons = wineCouponsDao.save(wineCoupons);
        return wineCoupons;
    }

    @Override
    public WineCoupons getWineCouponsById(long id) {
        return wineCouponsDao.getWineCouponsById(id);
    }

    @Override
    public List<WineCoupons> findUsableWineCoupons() {
        return wineCouponsDao.findUsableWineCoupons();
    }

    @Override
    public List<WineCoupons> findUsableWineCouponsForFixed() {
        return wineCouponsDao.findUsableWineCouponsForFixed();
    }

    @Override
    public PageDataList<WineCouponsModel> list(WineCouponsModel model) {
        return wineCouponsDao.list(model);
    }

    @Override
    public WineCoupons update(WineCouponsModel model) {
        //重新封装酒券方案
        WineCoupons wineCoupons = packageWineCoupons(model);
        wineCoupons.setId(model.getId());
        wineCoupons.setAddTime(model.getAddTime());
        wineCoupons.setAddIp(model.getAddIp());
        wineCoupons.setIssueNum(model.getIssueNum());
        wineCoupons.setTimeLimit(model.getTimeLimit());
        wineCoupons.setMinTenderAmount(model.getMinTenderAmount());
        wineCoupons.setMaxTenderAmount(model.getMaxTenderAmount());
        wineCoupons.setNumber(model.getNumber());
        //修改酒券方案
        wineCoupons = wineCouponsDao.update(wineCoupons);
        return wineCoupons;
    }

    @Override
    public void delete(long id) {
        //删除酒券方案
        wineCouponsDao.delete(id);
    }

    /**
     * 封装酒券
     * 重新封装酒券实体，防止属性误取值保存错误
     *
     * @param model WineCouponsModel
     * @return WineCoupons
     */
    @SuppressWarnings("static-access")
    public WineCoupons packageWineCoupons(WineCouponsModel model) {
    	WineCoupons wineCoupons = new WineCoupons();
        //固定金额
        if (model.getPayType() == model.WINE_COUPONS_PAY_FIXED_AMOUNT)
        	wineCoupons = new WineCoupons(model.getName(), model.getStatus(), model.getType(), model.getPayType(),
                    model.getAmount(), model.getValidDay(), model.getTotal(), model.getDescription(),
                    model.getTimeLimit(),model.getMinTenderAmount(),model.getMaxTenderAmount(),model.getNumber());
        return wineCoupons;
    }

    @Override
    public int changeStatus(byte status, byte prestatus, long id) {
        return wineCouponsDao.update(status,prestatus,id);
    }
}
